const axios = require('axios');

// 配置
const BASE_URL = 'http://localhost:3000/api';
const JWT_TOKEN = 'your_jwt_token_here'; // 替换为实际的JWT token

// 设置请求头
const headers = {
  'Authorization': `Bearer ${JWT_TOKEN}`,
  'Content-Type': 'application/json'
};

// 测试随机短剧数量获取
async function testRandomShortsCount() {
  try {
    console.log('🚀 开始测试随机短剧数量获取...\n');

    // 测试不同的count参数
    const testCounts = [1, 3, 5, 10];

    for (const count of testCounts) {
      console.log(`📊 测试获取 ${count} 条随机短剧...`);
      
      try {
        const response = await axios.get(
          `${BASE_URL}/shorts/random?count=${count}`,
          { headers }
        );
        
        const shorts = response.data;
        console.log(`✅ 成功获取 ${shorts.length} 条短剧`);
        
        // 验证数量
        if (shorts.length === count) {
          console.log(`✅ 数量正确: 请求${count}条，实际返回${shorts.length}条`);
        } else {
          console.log(`⚠️  数量不匹配: 请求${count}条，实际返回${shorts.length}条`);
        }
        
        // 显示短剧信息
        shorts.forEach((short, index) => {
          console.log(`\n   短剧${index + 1}: ${short.title}`);
          console.log(`     ID: ${short.id}`);
          console.log(`     分类: ${short.category?.name || '无'}`);
          console.log(`     方向: ${short.direction?.name || '无'}`);
          console.log(`     章节数: ${short.chapters?.length || 0}`);
          console.log(`     观看数: ${short.viewCount}`);
          console.log(`     点赞数: ${short.likeCount}`);
          console.log(`     收藏数: ${short.favoriteCount}`);
          
          // 显示章节信息（应该只有前2个）
          if (short.chapters && short.chapters.length > 0) {
            console.log(`     章节信息:`);
            short.chapters.forEach((chapter, chapterIndex) => {
              console.log(`       章节${chapterIndex + 1}: ${chapter.title} (积分: ${chapter.points})`);
            });
          }
        });
        
      } catch (error) {
        console.log(`❌ 获取${count}条短剧失败:`, error.response?.data?.message || error.message);
      }
      
      console.log('\n' + '─'.repeat(50));
    }

    // 测试边界情况
    console.log('🧪 测试边界情况...');
    
    // 测试count=0
    try {
      const response0 = await axios.get(
        `${BASE_URL}/shorts/random?count=0`,
        { headers }
      );
      console.log(`✅ count=0: 返回${response0.data.length}条短剧`);
    } catch (error) {
      console.log(`❌ count=0失败:`, error.response?.data?.message || error.message);
    }
    
    // 测试count=100（大数值）
    try {
      const response100 = await axios.get(
        `${BASE_URL}/shorts/random?count=100`,
        { headers }
      );
      console.log(`✅ count=100: 返回${response100.data.length}条短剧`);
    } catch (error) {
      console.log(`❌ count=100失败:`, error.response?.data?.message || error.message);
    }
    
    // 测试不传count参数（默认值）
    try {
      const responseDefault = await axios.get(
        `${BASE_URL}/shorts/random`,
        { headers }
      );
      console.log(`✅ 默认count: 返回${responseDefault.data.length}条短剧`);
    } catch (error) {
      console.log(`❌ 默认count失败:`, error.response?.data?.message || error.message);
    }

    console.log('\n✨ 随机短剧数量获取测试完成！');
    console.log('\n💡 测试要点:');
    console.log('   - 验证返回数量与请求数量一致');
    console.log('   - 验证每个短剧只返回前2个章节');
    console.log('   - 验证随机性（多次调用结果不同）');
    console.log('   - 验证边界情况处理');
    console.log('   - 验证真正的随机选择策略');

  } catch (error) {
    console.error('❌ 测试过程中发生错误:', error.message);
  }
}

// 运行测试
testRandomShortsCount().catch(console.error); 